//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc og Kasper
//*****************************************************************************


package view;

import control.ConnectHandler;
import control.ControlUtil;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Personale;

public class LoginPanel extends javax.swing.JFrame {

    private ConnectHandler connHandler;
    private ControlUtil controlUtil;
    
    public LoginPanel(ConnectHandler connectHandl) {
    
        connHandler = connectHandl;
        controlUtil = new ControlUtil(connHandler.getSession());
                
        initComponents();
        
        login.setEnabled(false);
        brugerCombo.setEnabled(false);
    }
     private void tilfoejPersonale(int type){
        Personale[] personale = controlUtil.getPersonaleHandler().hentPersonale(type);
        for (int i = 0; i < personale.length; i++) {
            brugerCombo.addItem(personale[i]);
        }
    
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        login = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        typeCombo = new javax.swing.JComboBox();
        brugerCombo = new javax.swing.JComboBox();
        logo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Holmegaard Heldagsskole Administration");
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(500, 200, 0, 0));
        setMaximumSize(new java.awt.Dimension(354, 320));
        setMinimumSize(new java.awt.Dimension(354, 320));
        setPreferredSize(new java.awt.Dimension(354, 320));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        getContentPane().setLayout(null);

        login.setText("LOGIN");
        login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });
        getContentPane().add(login);
        login.setBounds(110, 220, 133, 38);
        getContentPane().add(jLabel3);
        jLabel3.setBounds(82, 11, 0, 0);

        typeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bruger Type", "Lærer", "Pædagog", "Sektretær", "Administrator" }));
        typeCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeComboActionPerformed(evt);
            }
        });
        getContentPane().add(typeCombo);
        typeCombo.setBounds(90, 140, 170, 20);

        brugerCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bruger", "1", "2", "3" }));
        brugerCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brugerComboActionPerformed(evt);
            }
        });
        getContentPane().add(brugerCombo);
        brugerCombo.setBounds(90, 180, 170, 20);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/HeldagsskoleLogo.JPG"))); // NOI18N
        getContentPane().add(logo);
        logo.setBounds(60, 30, 221, 98);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginActionPerformed
        if(typeCombo.getSelectedIndex() != 0){
        Mainscreen ms = new Mainscreen((Personale) brugerCombo.getSelectedItem(), controlUtil);
        ms.setVisible(true);
        }
    }//GEN-LAST:event_loginActionPerformed

    private void typeComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeComboActionPerformed
        if(typeCombo.getSelectedIndex() != 0){
            brugerCombo.removeAllItems();
            brugerCombo.setEnabled(true);
            tilfoejPersonale(typeCombo.getSelectedIndex());
        }else{
            brugerCombo.setEnabled(false);
            brugerCombo.removeAllItems();
            brugerCombo.addItem("bruger");
        }
    }//GEN-LAST:event_typeComboActionPerformed

    private void brugerComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_brugerComboActionPerformed
        if(brugerCombo.getSelectedIndex() != 0){
            login.setEnabled(true);
        }
    }//GEN-LAST:event_brugerComboActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        
        System.exit(0);
        connHandler.getSession().close();
    }//GEN-LAST:event_formWindowClosed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox brugerCombo;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton login;
    private javax.swing.JLabel logo;
    private javax.swing.JComboBox typeCombo;
    // End of variables declaration//GEN-END:variables

    public static void main(String[] args) {

        ConnectHandler conHandl = null;
        try {
            conHandl = new ConnectHandler();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(LoginPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(LoginPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        LoginPanel lp = new LoginPanel(conHandl);
        lp.setVisible(true);
        
    }
}
